﻿using Microsoft.AspNetCore.Mvc;
using ERP.ERPSystem.DataAnalysis;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using System.Linq;

namespace ERP.Web.Controllers.DataAnalysis
{
    /// <summary>
    /// 数据分析-物料流转数据控制器-grx
    /// </summary>
    public class MaterialflowdataController : Controller
    {
        private readonly IMaterialflowdataService _materialflowdataService;

        public MaterialflowdataController(IMaterialflowdataService materialflowdataService)
        {
            _materialflowdataService = materialflowdataService;
        }

        /// <summary>
        /// 物料流转数据查询页面
        /// </summary>
        /// <returns></returns>
        public IActionResult MaterialflowdataQuery()
        {
            return View();
        }


        /// <summary>
        /// 获取柱状图数据
        /// </summary>
        /// <param name="documentType">单据类型</param>
        /// <returns></returns>
        public async Task<IActionResult> GetBarChartData(string documentType)
        {
            try
            {
                Dictionary<string, decimal> data;
                
                if (documentType == "产品生产")
                {
                    data = await _materialflowdataService.GetProductionWorkOrderBarChartData();
                }
                else
                {
                    // 默认或生产领料使用原有数据
                    data = await _materialflowdataService.GetMaterialFlowBarChartData();
                }
                
                return Json(new { code = 0, msg = "success", data = data });
            }
            catch (Exception ex)
            {
                return Json(new { code = -1, msg = ex.Message, data = new Dictionary<string, decimal>() });
            }
        }

        /// <summary>
        /// 获取折线图数据
        /// </summary>   
        /// <param name="documentType">单据类型</param>
        /// <returns></returns>
        public async Task<IActionResult> GetLineChartData(string documentType)
        {
            try
            {
                Dictionary<string, decimal> data;
                
                if (documentType == "产品生产")
                {
                    data = await _materialflowdataService.GetProductionWorkOrderLineChartData();
                }
                else
                {
                    // 默认或生产领料使用原有数据
                    data = await _materialflowdataService.GetMaterialFlowLineChartData();
                }
                
                return Json(new { code = 0, msg = "success", data = data });
            }
            catch (Exception ex)
            {
                return Json(new { code = -1, msg = ex.Message, data = new Dictionary<string, decimal>() });
            }
        }

        /// <summary>
        /// 获取饼状图数据
        /// </summary>
        /// <param name="documentType">单据类型</param>
        /// <returns></returns>
        public async Task<IActionResult> GetPieChartData(string documentType)
        {
            try
            {
                object data;
                
                if (documentType == "产品生产")
                {
                    data = await _materialflowdataService.GetProductionWorkOrderPieChartData();
                }
                else
                {
                    // 默认或生产领料使用原有数据
                    data = await _materialflowdataService.GetMaterialFlowPieChartData();
                }
                
                return Json(new { code = 0, msg = "success", data = data });
            }
            catch (Exception ex)
            {
                return Json(new { code = -1, msg = ex.Message, data = new { data = new List<object>(), total = 0 } });
            }
        }
    }
}
